// This should be done the right way asap.
//-----------------------------------------------------
#define S_W 640
#define S_H 480
#define W_TITLE "Ode-Demo-Game-Engine v0.000001 by bRue"
#define antialiasing 4
//------------------------------------------------------

#include <string>
#include <iostream>
#include <sstream>
#include <cstdlib>



#include "GLSL.hh"

#include <GL/gl.h>
#include <GL/glu.h>


#include <SDL/SDL.h>

#include "Renderable.hh"
#include "Touchable.hh"
#include "World.hh"
#include "Camera.hh"

using namespace std;

SDL_Surface*
sdl_init(int w, int h){
  if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) <0 )
    {
      printf("Unable to init SDL: %s\n", SDL_GetError());
      exit(1);
    } 
  
  SDL_Surface *screen;
  const SDL_VideoInfo *videoInfo;

  videoInfo = SDL_GetVideoInfo();

  if (antialiasing >= 2){
                SDL_GL_SetAttribute (SDL_GL_RED_SIZE, antialiasing);
                SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, antialiasing);
                SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, antialiasing);
                SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
                SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, antialiasing); 
                SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1);
    }

  screen=SDL_SetVideoMode(w,h,32, SDL_HWSURFACE|SDL_OPENGL|SDL_OPENGLBLIT|SDL_DOUBLEBUF);

  if (screen == NULL)
    {
      printf("Unable to set %dx%d video: %s\n", w, h, SDL_GetError());
      exit(1);
    }

  SDL_WM_SetCaption(W_TITLE, NULL);

  atexit(SDL_Quit);
  
  return screen;
}

void GL_Init(int w, int h){
  
  float ratio = (float) w / (float) h;
  
  glEnable (GL_DEPTH_TEST);

  glEnable(GL_NORMALIZE);
 
  glClearColor( 0., 0., 0., 0. );
 
  glViewport( 0, 0, w, h );
  
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity( );

  gluPerspective( 45.0, ratio, 1.0, 1000.0 );
  
}

int
main(int argc, char** argv){

  SDL_Surface* s;

  // The sdl_surface must be passed to the world constructor
  // if not, the program will exit.
  World w(s);

  int i;
  for (i=0; i < 1; i++){
      w.AddEntity(new Touchable(string("brue"), float((rand()-RAND_MAX/2)/float(RAND_MAX))*40, float((rand())/float(RAND_MAX))*20+30, float((rand())/float(RAND_MAX))*(-30)-20 , "modelos/1.md3", 500.0, 0,0,0));
    }
  
  w.AddCamera(new Camera(0,30,20, 0,0,-20,  0,1,0));
  

  s = sdl_init(S_W,S_H);

  GL_Init(S_W,S_H);

  std::cout << "GLSL: " << SetupGLSL() << std::endl;

  return(w.MainLoop());

}
